$(document).ready(function() {

    $("#partyActivityName").blur(function (){
        if ($("#partyActivityName").val().length==0) {
            $("#isNull").html("该项不能为空！").css("color","red");
        }else{
            $("#isNull").html("");
        }
    });


    $.ajax({
        url: "http://localhost:8080/partyActivity/queryPartyNameAndPON",
        type: "GET",
        async: true,
        data: "partyMemberAccount=" +1011,
        dataType: "json",
        success:function (data) {
            console.log(data);
            console.log(data.data);
            var partyName=data.data.partyName;
            var PON=data.data.PON;
            console.log("partyName="+partyName);
            console.log("PON="+PON);
            $("#partyName").val(partyName);
            $("#PON").val(PON);
            $("#mce_23").remove();//1
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });

});

$(document).on('click', '#add-btn', function() {


    var $PON= $("#PON").val();
    var $partyActivityName= $("#partyActivityName").val();
    var $partyActivityTime=getDate($("#partyActivityTime").val());
    var $partyActivityDescription=html_encode(tinyMCE.activeEditor.getContent());


    console.log($partyActivityName);
    console.log($partyActivityName==="");

    if ($partyActivityName===""){
        layer.msg("请输入活动名称");
        return;
    }

    //封装成对象提交
        var data = {
            partyOrganizationNum:$PON,
            partyActivityName:$partyActivityName,
            partyActivityTime:$partyActivityTime,
            partyActivityDescription:$partyActivityDescription,
        };
        $.ajax({
            url: "http://localhost:8080/partyActivity/addPartyActivity",
            type: "POST",
            async: false,
            data:JSON.stringify(data),
            dataType: "json",
            contentType:"application/json",
            success:function () {
                layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                layer.alert('提交成功!', function(){
                    var index = parent.layer.getFrameIndex(window.name);
                    setTimeout(function(){
                        parent.layer.close(index)
                    }, 2000);
                    parent.location.reload()
                });
            },
            error:function () {
                layer.msg("网络在开小差~稍后重试");
            }

        });


});



function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}

function getDate(str) {
    if(str.indexOf("-") >= 0 ){
        return str;
    }else {
        var arr=str.split(" ");
        console.log(arr[1]);
        var day=arr[0];
        var month=arr[1];
        var year=arr[2];
        if(month=='January'){
            month="01";
        }else if(month=='February'){
            month="02";
        }else if(month=='March'){
            month="03";
        }else if(month=='April'){
            month="04";
        }else if(month=='May'){
            month="05";
        }else if(month=='June'){
            month="06";
        }else if(month=='July'){
            month="07";
        }else if(month=='August'){
            month="08";
        }else if(month=='September'){
            month="09";
        }else if(month=='October'){
            month="10";
        }else if(month=='November'){
            month="11";
        }else if(month=='December'){
            month="12";
        }
        return year+"-"+month+"-"+day;
    }
}

/*html转码*/
function html_encode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s = str.replace(/&/g, "")
        .replace("<p>", "")
        .replace("<\p>", "")
        .replace(/\'/g, "")
        .replace(/\"/g, "")
        .replace(/\n/g, "\n");
    return s;
}

